package edu.unice.simul.vue.components.simul;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import edu.unice.simul.controleur.interfaces.PlayPauseControlerI;

/**
 * Panneau de pause / lecture
 * 
 * @author Romain Ciaccafava
 * 
 */
public class PlayPausePanel extends JPanel {
	private static final long serialVersionUID = 5169360998596693846L;

	private PlayPauseControlerI controleur;
	private ImageIcon playImage = new ImageIcon("./res/play.png");
	private ImageIcon pauseImage = new ImageIcon("./res/pause.png");
	private JToggleButton play = new JToggleButton(playImage);
	private JToggleButton pause = new JToggleButton(pauseImage);

	public PlayPausePanel(PlayPauseControlerI controleur) {
		this.controleur = controleur;

		buildUI();
		addListeners();
	}

	/**
	 * Construit l'interface
	 */
	private void buildUI() {
		setLayout(new GridLayout(1, 2));
		add(play);
		add(pause);

		play.setEnabled(false);

		play.setToolTipText("Lecture");
		play.setToolTipText("Pause");

		setBorder(BorderFactory.createTitledBorder(""));
	}

	/**
	 * Ajoute les écouteur aux boutons
	 */
	private void addListeners() {
		play.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (play.isSelected()) {
					pause.setSelected(false);
					play.setEnabled(false);
					pause.setEnabled(true);
					controleur.play();
				}
			}
		});

		pause.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (pause.isSelected()) {
					play.setSelected(false);
					play.setEnabled(true);
					pause.setEnabled(false);
					controleur.pause();
				}
			}
		});

		play.setSelected(true);
	}

	/**
	 * Vérouille le panneau
	 */
	public void freeze() {
		play.setEnabled(false);
		pause.setEnabled(false);
	}
}
